package com.zsyh.jiujiu.home;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.againstsky.verificationcode.VerificationCodeView;
import com.android.tu.loadingdialog.LoadingDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.utils.EmptyUtil;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.DemoHelper;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.LoginBean;
import com.zsyh.jiujiu.bean.YanzhengCodeBean;
import com.zsyh.jiujiu.bean.ZhuCeBean;
import com.zsyh.jiujiu.db.DemoDBManager;
import com.zsyh.jiujiu.net.MyObserver;
import com.zsyh.jiujiu.utils.TimeTextLogin;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class LoginActivity extends BaseActivity {

    @BindView(R.id.imagelogin)
    ImageView imagelogin;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.xianlogin)
    View xianlogin;
    @BindView(R.id.relogin)
    RelativeLayout relogin;
    @BindView(R.id.tv_gister)
    TextView tvGister;
    @BindView(R.id.xiangister)
    View xiangister;
    @BindView(R.id.register)
    RelativeLayout register;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.tv_mima)
    TextView tvMima;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.login)
    TextView login;
    @BindView(R.id.redenglu)
    RelativeLayout redenglu;
    @BindView(R.id.wanjipassord)
    TextView wanjipassord;
    @BindView(R.id.tv_registerphone)
    TextView tvRegisterphone;
    @BindView(R.id.registerphone)
    EditText registerphone;
    @BindView(R.id.tv_registermima)
    TextView tvRegistermima;
    @BindView(R.id.reegisterpassword)
    EditText reegisterpassword;
    @BindView(R.id.tuxing)
    TextView tuxing;
    @BindView(R.id.registeryanzheng)
    EditText registeryanzheng;
    @BindView(R.id.code_view)
    VerificationCodeView codeView;
    @BindView(R.id.tv_phone_yan)
    TextView tvPhoneYan;
    @BindView(R.id.btn_get_code)
    Button btnGetCode;
    @BindView(R.id.re_yanzhen)
    RelativeLayout reYanzhen;
    @BindView(R.id.registershoujiyan)
    EditText registershoujiyan;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.rezhuce)
    RelativeLayout rezhuce;
    private TimeTextLogin mTimeText;
    private String yanCode;


    @Override
    protected int initLayout() {
        return R.layout.activity_logo;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        tvLogin.setTextSize(17);
        tvGister.setTextSize(15);
        redenglu.setVisibility(View.VISIBLE);
        rezhuce.setVisibility(View.GONE);
        xianlogin.setBackgroundColor(getResources().getColor(R.color.white));
        xiangister.setBackgroundColor(getResources().getColor(R.color.touming));
    }

    @Override
    protected void initEvent() {

    }



    @OnClick({R.id.relogin, R.id.register, R.id.login, R.id.wanjipassord, R.id.btn_get_code, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.relogin:
                tvLogin.setTextSize(17);
                tvGister.setTextSize(15);
                redenglu.setVisibility(View.VISIBLE);
                rezhuce.setVisibility(View.GONE);
                xianlogin.setBackgroundColor(getResources().getColor(R.color.white));
                xiangister.setBackgroundColor(getResources().getColor(R.color.touming));

                break;
            case R.id.register:
                tvLogin.setTextSize(15);
                tvGister.setTextSize(17);
                redenglu.setVisibility(View.GONE);
                rezhuce.setVisibility(View.VISIBLE);
                xianlogin.setBackgroundColor(getResources().getColor(R.color.touming));
                xiangister.setBackgroundColor(getResources().getColor(R.color.white));
                break;
            case R.id.login:

                if(EmptyUtil.isEmpty(phone.getText().toString())){
                    ToastUtils.showShort("手机号不能为空");
                    return;
                }else if(isPhoneNumber(phone.getText().toString())==false){
                    ToastUtils.showShort("手机号不正确");
                    return;
                }

                if(EmptyUtil.isEmpty(password.getText().toString())){
                    ToastUtils.showShort("密码不能为空");
                    return;
                }

                loginDenglu(phone.getText().toString(),password.getText().toString());
                break;
            case R.id.wanjipassord:

                ActivityUtils.startActivity(forgePassswardActivity.class);
                break;
            case R.id.btn_get_code:

                if(EmptyUtil.isEmpty(registerphone.getText().toString())){
                    ToastUtils.showShort("手机号不能为空");
                    return;
                }else if(isPhoneNumber(registerphone.getText().toString())==false){
                    ToastUtils.showShort("手机号不正确");
                    return;
                }

                if(EmptyUtil.isEmpty(registeryanzheng.getText().toString())){
                    ToastUtils.showShort("图形验证码不能为空");
                    return;
                }

                if(registeryanzheng.getText().toString().toLowerCase().equals(codeView.getVerificationCode().toLowerCase())){
                    Yzm(registerphone.getText().toString());
                }else{
                    ToastUtils.showShort("图形验证码不正确");
                }
                break;
            case R.id.next:
                if(EmptyUtil.isEmpty(registerphone.getText().toString())){
                    ToastUtils.showShort("手机号不能为空");
                    return;
                }else if(isPhoneNumber(registerphone.getText().toString())==false){
                    ToastUtils.showShort("手机号不正确");
                    return;
                }

                if(EmptyUtil.isEmpty(reegisterpassword.getText().toString())){
                    ToastUtils.showShort("登录密码不能为空");
                    return;
                }

                if(EmptyUtil.isEmpty(registeryanzheng.getText().toString())){
                    ToastUtils.showShort("图形验证码不能为空");
                    return;
                }

                if(EmptyUtil.isEmpty(registershoujiyan.getText().toString())){
                    ToastUtils.showShort("手机验证码不能为空");
                    return;
                }else if(!yanCode.equals(registershoujiyan.getText().toString())){
                    ToastUtils.showShort("手机验证码不正确");
                    return;
                }

                registeer(registerphone.getText().toString(),reegisterpassword.getText().toString());

                break;
        }
    }

    /**
     * 注册
     */
    public void registeer(String phone,String password){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .register(phone,password)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("denglu",JsonStr+"");
                        Gson gson=new Gson();
                        ZhuCeBean zhuCeBean=gson.fromJson(JsonStr,ZhuCeBean.class);
                        ToastUtils.showShort(zhuCeBean.getMsg());
                        tvLogin.setTextSize(17);
                        tvGister.setTextSize(15);
                        redenglu.setVisibility(View.VISIBLE);
                        rezhuce.setVisibility(View.GONE);
                        xianlogin.setBackgroundColor(getResources().getColor(R.color.white));
                        xiangister.setBackgroundColor(getResources().getColor(R.color.touming));

                    }
                });

    }

    /**
     * 验证码
     */

    public void Yzm(String phone){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .sendTextMsg(phone)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("yanzgengma",JsonStr+"");
                        Gson gson=new Gson();
                        YanzhengCodeBean zhuCeBean=gson.fromJson(JsonStr,YanzhengCodeBean.class);
                        mTimeText = new TimeTextLogin(60000, 1000, btnGetCode);
                        mTimeText.start();
                        yanCode=zhuCeBean.getData();


                    }
                });
    }

    /**
     * 登录
     */
    public void loginDenglu(String phone,String password){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .login(phone,password)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,true) {
                    @Override
                    protected void onError(String errorMsg) {
                        Log.e("dengluerror",errorMsg+"");
                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("denglu",JsonStr+"");
                        Gson gson=new Gson();
                        LoginBean loginBean=gson.fromJson(JsonStr, LoginBean.class);
                        SPUtils.put(thisActivity,"userid",loginBean.getData().getId());
                        loginHX(loginBean.getData().getId(),loginBean.getData().getNickname());
                        thisActivity.finish();
                    }
                });

    }

    protected void loginHX(String userId,String nickName) {
        //DemoDBManager.getInstance().closeDB();

        // reset current user name before login
        //DemoHelper.getInstance().setCurrentUserName(nickName);

        EMClient.getInstance().login(userId, "123456", new EMCallBack() {
            @Override
            public void onSuccess() {

                //此方法传入一个字符串String类型的参数，返回成功或失败的一个Boolean类型的返回值
                try {
                    EMClient.getInstance().pushManager().updatePushNickname(nickName);
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                Log.e("hx", "登录聊天服务器成功！");


            }

            @Override
            public void onError(int i, String s) {


            }

            @Override
            public void onProgress(int i, String s) {


            }
        });

        DemoHelper.getInstance().getUserProfileManager().asyncGetCurrentUserInfo();


    }
}
